clear 

* set directory 
* cd "directory"

* set paths
global datafolder "data"
global outputfolder "output"

********************************************************************************
*			Table 1: Summary statistics
********************************************************************************

	use "$datafolder\maindata.dta", clear
	
	keep if datem >= 660
	
	* Report collateralization rate in percentage
	replace has_coll_m_any = has_coll_m_any * 100
	
	* Report nominal variables in millions
	replace balance = balance/1000000
	
	label variable actual_rate "\hspace*{0.3cm}Actual Rate (\%)"
	label variable theoretical_rate "\hspace*{0.3cm}Breakeven Rate (\%)"
	label variable Dif "\hspace*{0.3cm}Markup (\%)"
	label variable balance "\hspace*{0.3cm}Loan Amount (million \euro)"
	label variable maturity_range "\hspace*{0.3cm}Maturity (years)"
	label variable has_coll_m_any "\hspace*{0.3cm}Collateralized (\%)"
	
	local n_fmt = "fmt(2 2 2 2 2 2)"
			
	 * Account-level variables
	 estpost tabstat actual_rate theoretical_rate Dif balance maturity_range has_coll_m_any, /* 
	 */  statistics( count mean sd p5 p25 median p75 p95) columns(statistics)
	 esttab using "$outputfolder\table_1_A.tex",  /* 
	 */ cells("count(label(Count)) mean(`n_fmt' label(Mean)) sd(`n_fmt' label(St. Dev)) p5(`n_fmt' label(P5)) p25(`n_fmt' label(P25)) p50(`n_fmt' label(P50)) p75(`n_fmt' label(P75)) p95(`n_fmt' label(P95))") /* 
	 */  replace nonum noobs label  
	 
	 
	
	* Customer-level variables
	collapse (mean) OROA Deposits TotalAssets ratio_CurrentLiabilities, ///
	by(customerid)
	
	* Report nominal variables in millions
	replace Deposits = Deposits/1000000
	replace TotalAssets = TotalAssets/1000000
	
	label variable OROA "\hspace*{0.3cm}OROA (\%)"
	label variable Deposits "\hspace*{0.3cm}Deposits (million \euro)"
	label variable TotalAssets "\hspace*{0.3cm}Total Assets (million \euro)"
	label variable ratio_CurrentLiabilities "\hspace*{0.3cm}Liabilities/Assets (\%)"
	
	local n_fmt = "fmt(2 2 2 2)"
			
	estpost tabstat OROA Deposits TotalAssets ratio_CurrentLiabilities, /* 
	 */  statistics( mean sd p5 p25 median p75 p95) columns(statistics)
	esttab using "$outputfolder\table_1_B.tex",  /* 
	*/ cells("mean(`n_fmt' label(Mean)) sd(`n_fmt' label(St. Dev)) p5(`n_fmt' label(P5)) p25(`n_fmt' label(P25)) p50(`n_fmt' label(P50)) p75(`n_fmt' label(P75)) p95(`n_fmt' label(P95))") /* 
	 */  replace nonum noobs label		
	 



